package com.changli06._collections;

import org.junit.Test;

import java.util.*;

public class collectionsTest {

    @Test
    public void test1() {
        List list = Arrays.asList(54,345,3445,57657,1231,5465);
        System.out.println(list);
        Collections.reverse(list);  //反转
        System.out.println(list);
        Collections.shuffle(list);
        System.out.println(list);

    }
    @Test
    public void test2() {
        List list = Arrays.asList(54,345,3445,57657,1231,5465);
        System.out.println(list);
        Collections.shuffle(list); //随机排列
        System.out.println(list);

    }
    @Test
    public void test3() {
        List list = Arrays.asList(54,345,3445,57657,1231,5465);
        System.out.println(list);
        Collections.sort(list); //默认升序排列
        System.out.println(list);


    }
    @Test
    public void test4() {
        List list = Arrays.asList(54,345,3445,57657,1231,5465);
        System.out.println(list);
        Comparator comparator = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Integer && o2 instanceof Integer) {
                    return -((Integer) o1).compareTo((Integer) o2);
                }
                throw new RuntimeException("类型不匹配");
            }
        };
        Collections.sort(list, comparator);  //降序排列、定制排序
        System.out.println(list);


    }
    @Test
    public void test5() {
        List list = Arrays.asList(54,345,3445,57657,1231,5465);
        System.out.println(list);
        Collections.swap(list,2,5);
        System.out.println(list);



    }


    @Test
    public void test6() {
        List list = Arrays.asList(54,345,3445,57657,1231,5465);
        System.out.println(list);
        List dest = Arrays.asList(new Object [list.size()]);
        Collections.copy(dest,list);
        System.out.println(dest);

    }
    @Test
    public void test7() {
        List list = new ArrayList();
        list.add(11);
        list.add(22);
        System.out.println(list);
        List list1 = Collections.unmodifiableList(list);
        //只能读，不能添加和删除
//        list1.add(55);
        System.out.println(list.get(0));

    }
}
